﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Admin
{
    public partial class Change_OT : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable OT = GetDataOT();
                txt_Changeot.Text = OT.Rows[0]["RateOT_Num"].ToString();

                DataTable Absence = GetDataAbsence();
                txt_Absence.Text = Absence.Rows[0]["Rate_AbsenceNum"].ToString();

                DataTable Other = GetDataOther();
                txt_Other.Text = Other.Rows[0]["Rate_OtherNum"].ToString();
            }
        }

        protected void bt_editot_Click(object sender, EventArgs e)
        {
            txt_Changeot.ReadOnly = false;
            bt_saveot.Enabled = true;
            bt_editot.Enabled = false;
            txt_Changeot.Focus();
        }

        protected void bt_saveot_Click(object sender, EventArgs e)
        {
            if (txt_Changeot.Text.Length > 0)
            {
                 InsertsRateOT();
                GV_ShowrankOT.DataBind();
            }
            DataTable OT = GetDataOT();
            txt_Changeot.Text = OT.Rows[0]["RateOT_Num"].ToString();
            txt_Changeot.ReadOnly = true;
            bt_saveot.Enabled = false;
        }
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            return value;
        }
        public void InsertsRateOT()
        {
            string sqltext = @"insert into [TB_RateOT] 
                                   ( [RateOT_Date]
                                    ,[RateOT_Num]
                                    ,[Act_Admin])
                                   values (@Date,@Num,@Admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
           // string[] Date = dateEdit.Value.ToString().Split(' ');
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(txt_Changeot.Text));
            sqlcomm.Parameters.AddWithValue("@Admin", GetDataValue("Admin"));
           // sqlcomm.Parameters.AddWithValue("@CheckIn_Time", GetDataValue(dateEdit.Value));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsRateAbsence()
        {
            string sqltext = @"insert into [TB_RateAbsence] 
                                   ( [Rate_AbsenceDate]
                                    ,[Rate_AbsenceNum]
                                    ,[Act_Admin])
                                   values (@Date,@Num,@Admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            // string[] Date = dateEdit.Value.ToString().Split(' ');
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(txt_Absence.Text));
            sqlcomm.Parameters.AddWithValue("@Admin", GetDataValue("Admin"));
            // sqlcomm.Parameters.AddWithValue("@CheckIn_Time", GetDataValue(dateEdit.Value));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsRateOther()
        {
            string sqltext = @"insert into [TB_RateOther] 
                                    ([Rate_OtherDate]
                                    ,[Rate_OtherNum]
                                    ,[Act_Admin])
                                   values (@Date,@Num,@Admin)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            // string[] Date = dateEdit.Value.ToString().Split(' ');
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(txt_Other.Text));
            sqlcomm.Parameters.AddWithValue("@Admin", GetDataValue("Admin"));
            // sqlcomm.Parameters.AddWithValue("@CheckIn_Time", GetDataValue(dateEdit.Value));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public DataTable GetDataOT()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [RateOT_Date],[RateOT_Num] FROM [TB_RateOT] ORDER BY [RateOT_Date] DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        public DataTable GetDataAbsence()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [Rate_AbsenceDate],[Rate_AbsenceNum] FROM [TB_RateAbsence] ORDER BY [Rate_AbsenceDate] DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        public DataTable GetDataOther()
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT TOP 1 [Rate_OtherDate],[Rate_OtherNum] FROM [TB_RateOther] ORDER BY [Rate_OtherDate] DESC";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        protected void bt_editAbsence_Click(object sender, EventArgs e)
        {
            txt_Absence.ReadOnly = false;
            bt_saveabsence.Enabled = true;
            bt_editAbsence.Enabled = false;
            txt_Absence.Focus();
        }

        protected void bt_saveabsence_Click(object sender, EventArgs e)
        {
            if (txt_Absence.Text.Length > 0)
            {
               InsertsRateAbsence();
                GV_Absence.DataBind();
            }
            DataTable Absence = GetDataAbsence();
            txt_Absence.Text = Absence.Rows[0]["Rate_AbsenceNum"].ToString();
            txt_Absence.ReadOnly = true;
            bt_saveabsence.Enabled = false;
        }

        protected void bt_EditOther_Click(object sender, EventArgs e)
        {
            txt_Other.ReadOnly = false;
            bt_SaveOther.Enabled = true;
            bt_EditOther.Enabled = false;
            txt_Other.Focus();
        }

        protected void bt_SaveOther_Click(object sender, EventArgs e)
        {
            if (txt_Other.Text.Length >0)
            {
               InsertsRateOther(); 
                Gv_Other.DataBind();
            }
            DataTable Other = GetDataOther();
            txt_Other.Text = Other.Rows[0]["Rate_OtherNum"].ToString();
            txt_Other.ReadOnly = true;
            bt_SaveOther.Enabled = false;
        }
    }
}